/**
 *
 */
package rgbos.forum.webClient;

import java.util.Date;
import java.util.Map;

import rgbos.forum.domain.Post;
import rgbos.forum.exceptions.UserExeception;

import jaxcent.HtmlInputButton;
import jaxcent.HtmlInputSubmit;
import jaxcent.HtmlInputText;
import jaxcent.HtmlPara;
import jaxcent.HtmlTextArea;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;


/**
 * @author Ofir Dagan
 *
 */
public class QuoteMsg extends JaxcentPage {
	
	private String author;
	private HtmlPara para = new HtmlPara(this, "header");
	HtmlPara footpara_ = new HtmlPara(this,"footerPara");
	private HtmlInputText titleInput = new HtmlInputText(this,"titleP");
	private HtmlTextArea bodyInput = new HtmlTextArea(this,"bodyP");
	private WebClientHandler handler_ = WebClientHandler.getWebClientHandler();;
	private int pid;
	private int topicID;
	private boolean flag = false;
	
	private HtmlInputButton cancel = new HtmlInputButton(this, "cancel") {		
		protected void onClick( Map pageData ) {
			try {
				goBack();
			} catch (Jaxception e) {
				e.printStackTrace();
			}
		}
	};
	
	
	private HtmlInputSubmit submitButton = new HtmlInputSubmit(this, "newMessSubmit") {		
		protected void onClick( Map pageData ) {
			if (flag)
				return;
			flag = true;
			String title = handler_.escape((String) pageData.get("title"));
			if (title.equals("")) {
				showMessageDialog("Please enter a title before submiting");
				flag = false;
				return;
			}
			String body = handler_.escape((String) pageData.get("body"));
			Post p = new Post(0,title,author,new Date(),body);
			if(handler_.getControl().addNewPost(p, topicID) < 0) {
				showMessageDialog("Can't edit post. Title or body too long or contain illegal characters");
				flag = false;
				return;
			}				
			try {
				navigate("/showPosts.html?topicId="+topicID);
			} catch (Jaxception e) {
				e.printStackTrace();
			}
			flag = false;
		}
	};
	
	public QuoteMsg(){
		super();
		this.setFormSaveEnabled(false);
	}
	
	protected void onUnload() {
		try {
			Map sessionData = getAllSessionData(false);
			handler_.unload(sessionData);			
		} catch (Jaxception e) {
			e.printStackTrace();
		}
	}
	
	protected void onLoad() {
		try {
			submitButton.setValue("Post");
			cancel.setValue("Cancel");
			Map sessionData = getAllSessionData( false );
			String type = handler_.getClientType(sessionData);
			if (type.equals("Guest")) {
				showMessageDialog("Please login or register to add posts");
				navigate("/showTopics.html");
			}
			final String tPath = getCurrentPath();
			splitPathVar(tPath);
			Post p= handler_.getControl().getPostById(pid);
			titleInput.setValue(handler_.unescape("Quote: "+p.getTitle_()));
			bodyInput.setValue(handler_.unescape(p.getAuthorName()+" wrote: \""+ p.getBody_()+ "\""));			
			this.author = (String)sessionData.get("uName");		
			handler_.DisplayHeader(para, sessionData, this); //must be before display footer!
			handler_.displayFooter(footpara_, sessionData, this);			
		} catch (Jaxception e) {
			e.printStackTrace();
		}
	}



	private void splitPathVar(final String tPath) {
		String[] split= tPath.split("html");
		String[] splitVar = split[1].split("&");
		String[] topicid= splitVar[0].split("=");
		String[] postId =splitVar[1].split("=");
		topicID = Integer.valueOf(topicid[1]);
		pid=Integer.valueOf(postId[1]);
	}
}
